/**
 * Created on [2022-11-11] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.common.exception.ServiceException;
import com.sunnada.btfvilg.entity.Site;
import com.sunnada.btfvilg.mapper.SiteMapper;
import com.sunnada.btfvilg.model.request.SiteParamVo;
import com.sunnada.btfvilg.model.request.UserParamVo;
import com.sunnada.btfvilg.model.response.SiteListVo;
import com.sunnada.btfvilg.model.response.UserVo;
import com.sunnada.btfvilg.service.SiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements SiteService {
    @Autowired(required = false)
    private SiteMapper siteMapper;
    @Override
    public void updateSite(SiteParamVo siteVo) {
        Site site1=new Site();
        site1.setSid(siteVo.getSid());
        site1.setSpassword(siteVo.getSpassword());
        site1.setSname(siteVo.getSname());
        site1.setPnum(siteVo.getPnum());
        site1.setPmax(siteVo.getPmax());
        site1.setSphone(siteVo.getSphone());
        site1.setDnum(siteVo.getDnum());
        siteMapper.updateById(site1);
    }

    @Override
    public UserVo siteLogin(UserParamVo user) {
        QueryWrapper<Site> qw = new QueryWrapper<>();
        qw.eq("sname", user.getUsername());
        Site site = getOne(qw);
        if (site == null) {
            throw new ServiceException("用户不存在");
        }
        String inputPassword = user.getPassword();
        if (Objects.equals(inputPassword, site.getSpassword())) {
            return new UserVo(site.getSid(),site.getSname());
        } else {
            throw new ServiceException("用户名或密码错误");
        }
    }

    @Override
    public Site getSiteById(String sid) {
        return siteMapper.selectById(sid);
    }

    @Override
    public UserVo login(UserParamVo siteVo) {
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sname", siteVo.getUsername());
        Site site = getOne(queryWrapper);
        if (site == null) {
            throw new ServiceException("网点不存在");
        }
        String inputPassword = siteVo.getPassword();
        if (Objects.equals(inputPassword, site.getSpassword())) {
            return new UserVo(site.getSid(),site.getSname());
        } else {
            throw new ServiceException("网点名或密码错误");
        }
    }

    @Override
    public SiteListVo getSiteList(Integer page, Integer numPerPage) {
        SiteListVo siteList = new SiteListVo();
        QueryWrapper<Site> qw = new QueryWrapper<>();
        Integer count = siteMapper.selectCount(qw);
        siteList.setTotal(count);
        List<Site> sites= null;
        sites = siteMapper.getSiteList( (page - 1) * numPerPage, numPerPage);
        siteList.setSiteList(sites);
        siteList.setPage(page);
        siteList.setNumPerPage(numPerPage);
        return siteList;
    }

    @Override
    public UserVo addSite(SiteParamVo siteVo) {
        Site site = new Site();
        site.setSid(UUID.randomUUID().toString().replaceAll("-",""));
        site.setSname(siteVo.getSname());
        site.setPnum(siteVo.getPnum());
        site.setPmax(siteVo.getPmax());
        site.setSphone(siteVo.getSphone());
        site.setDnum(siteVo.getDnum());
        site.setSpassword(siteVo.getSpassword());
        siteMapper.insert(site);
        UserVo reg = new UserVo(site.getSid(),site.getSname());
        return reg;
    }

    @Override
    public void delSite(String sid) {
        siteMapper.deleteById(sid);
    }

}
